Image bounding box can be specified instead of width, e.g. as
[lhc/web/wiklou.git] / includes / Image.php
1 <?php
2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
5
6 class Image
7 {
8 /* private */
9 var $name, # name of the image
10 $imagePath, # Path of the image
11 $url, # Image URL
12 $title, # Title object for this image. Initialized when needed.
13 $fileExists, # does the image file exist on disk?
14 $historyLine, # Number of line to return by nextHistoryLine()
15 $historyRes, # result of the query for the image's history
16 $width, # \
17 $height, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
18 $type, # |
19 $attr; # /
20
21
22
23 function Image( $name )
24 {
25 global $wgUploadDirectory;
26
27 $this->name = $name;
28 $this->title = Title::makeTitle( Namespace::getImage(), $this->name );
29 //$this->imagePath = wfImagePath( $name );
30 $hash = md5( $this->title->getDBkey() );
31 $this->imagePath = $wgUploadDirectory . "/" . $hash{0} . "/" .substr( $hash, 0, 2 ) . "/{$name}";
32
33 $this->url = $this->wfImageUrl( $name );
34
35 if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended
36 {
37 list($this->width, $this->height, $this->type, $this->attr) = getimagesize( $this->imagePath );
38 }
39 $this->historyLine = 0;
40 }
41
42 function newFromTitle( $nt )
43 {
44 $img = new Image( $nt->getDBKey() );
45 $img->title = $nt;
46 return $img;
47 }
48
49 function getName()
50 {
51 return $this->name;
52 }
53
54 function getURL()
55 {
56 return $this->url;
57 }
58
59 function getImagePath()
60 {
61 return $this->imagePath;
62 }
63
64 function getWidth()
65 {
66 return $this->width;
67 }
68
69 function getHeight()
70 {
71 return $this->height;
72 }
73
74 function getType()
75 {
76 return $this->type;
77 }
78
79 function getEscapeLocalURL()
80 {
81 return $this->title->escapeLocalURL();
82 }
83
84 function wfImageUrl( $name )
85 {
86 global $wgUploadPath;
87 $hash = md5( $name );
88
89 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
90 substr( $hash, 0, 2 ) . "/{$name}";
91 return wfUrlencode( $url );
92 }
93
94
95 function exists()
96 {
97 return $this->fileExists;
98 }
99
100 function thumbUrl( $width, $subdir="thumb" ) {
101 global $wgUploadPath;
102
103 $name = $this->thumbName( $width );
104 $hash = md5( $name );
105 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
106
107 return wfUrlencode($url);
108 }
109
110 function thumbName( $width ) {
111 return $width."px-".$this->name;
112 }
113
114 //**********************************************************************
115 // Create a thumbnail of the image having the specified width.
116 // The thumbnail will not be created if the width is larger than the
117 // image's width. Let the browser do the scaling in this case.
118 // The thumbnail is stored on disk and is only computed if the thumbnail
119 // file does not exist OR if it is older than the image.
120 function createThumb( $width ) {
121 global $wgUploadDirectory;
122 global $wgImageMagickConvertCommand;
123 global $wgUseImageMagick;
124 global $wgUseSquid, $wgInternalServer;
125 $thumbName = $this->thumbName( $width );
126 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
127 $thumbUrl = $this->thumbUrl( $width );
128
129 if ( ! $this->exists() )
130 {
131 # If there is no image, there will be no thumbnail
132 return "";
133 }
134
135 # Sanity check $width
136 $width = IntVal( $width );
137 if( $width <= 0 ) {
138 # BZZZT
139 return "";
140 }
141
142 if( $width > $this->width ) {
143 # Don't make an image bigger than the source
144 return $this->getURL();
145 }
146
147 if ( (! file_exists( $thumbPath ) )
148 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
149 # Squid purging
150 if ( $wgUseSquid ) {
151 $urlArr = Array(
152 $wgInternalServer.$thumbUrl
153 );
154 wfPurgeSquidServers($urlArr);
155 }
156
157 if ( $wgUseImageMagick ) {
158 # use ImageMagick
159 $cmd = $wgImageMagickConvertCommand .
160 " -quality 85 -geometry {$width} ".
161 escapeshellarg($this->imagePath) . " " .
162 escapeshellarg($thumbPath);
163 $conv = shell_exec( $cmd );
164 } else {
165 # Use PHP's builtin GD library functions.
166 #
167 # First find out what kind of file this is, and select the correct
168 # input routine for this.
169
170 switch( $this->type ) {
171 case 1: # GIF
172 $src_image = imagecreatefromgif( $this->imagePath );
173 break;
174 case 2: # JPG
175 $src_image = imagecreatefromjpeg( $this->imagePath );
176 break;
177 case 3: # PNG
178 $src_image = imagecreatefrompng( $this->imagePath );
179 break;
180 case 15: # WBMP for WML
181 $src_image = imagecreatefromwbmp( $this->imagePath );
182 break;
183 case 16: # XBM
184 $src_image = imagecreatefromxbm( $this->imagePath );
185 break;
186 default:
187 return "Image type not supported";
188 break;
189 }
190 $height = floor( $this->height * ( $width/$this->width ) );
191 $dst_image = imagecreatetruecolor( $width, $height );
192 imagecopyresampled( $dst_image, $src_image,
193 0,0,0,0,
194 $width, $height, $this->width, $this->height );
195 switch( $this->type ) {
196 case 1: # GIF
197 case 3: # PNG
198 case 15: # WBMP
199 case 16: # XBM
200 #$thumbUrl .= ".png";
201 #$thumbPath .= ".png";
202 imagepng( $dst_image, $thumbPath );
203 break;
204 case 2: # JPEG
205 #$thumbUrl .= ".jpg";
206 #$thumbPath .= ".jpg";
207 imageinterlace( $dst_image );
208 imagejpeg( $dst_image, $thumbPath, 95 );
209 break;
210 default:
211 break;
212 }
213 imagedestroy( $dst_image );
214 imagedestroy( $src_image );
215
216
217 }
218 #
219 # Check for zero-sized thumbnails. Those can be generated when
220 # no disk space is available or some other error occurs
221 #
222 $thumbstat = stat( $thumbPath );
223 if( $thumbstat["size"] == 0 )
224 {
225 unlink( $thumbPath );
226 }
227
228 }
229 return $thumbUrl;
230 } // END OF function createThumb
231
232 //**********************************************************************
233 // Return the image history of this image, line by line.
234 // start with current version, than old versions.
235 // use $this->historyLine to check which line to return:
236 // 0 return line for current version
237 // 1 query for old versions, return first one
238 // 2, ... return next old version from above query
239 function nextHistoryLine()
240 {
241 $fname = "Image::nextHistoryLine()";
242
243 if ( $this->historyLine == 0 ) // called for the first time, return line from cur
244 {
245 $sql = "SELECT img_size,img_description,img_user," .
246 "img_user_text,img_timestamp, '' AS oi_archive_name FROM image WHERE " .
247 "img_name='" . wfStrencode( $this->title->getDBkey() ) . "'";
248 $this->historyRes = wfQuery( $sql, DB_READ, $fname );
249
250 if ( 0 == wfNumRows( $this->historyRes ) ) { return FALSE; }
251
252 } else if ( $this->historyLine == 1 )
253 {
254 $sql = "SELECT oi_size AS img_size, oi_description AS img_description," .
255 "oi_user AS img_user," .
256 "oi_user_text AS img_user_text, oi_timestamp AS img_timestamp , oi_archive_name FROM oldimage WHERE " .
257 "oi_name='" . wfStrencode( $this->title->getDBkey() ) . "' " .
258 "ORDER BY oi_timestamp DESC";
259 $this->historyRes = wfQuery( $sql, DB_READ, $fname );
260 }
261 $this->historyLine ++;
262
263 return wfFetchObject( $this->historyRes );
264 }
265
266 function resetHistory()
267 {
268 $this->historyLine = 0;
269 }
270
271
272 } //class
273